package com.zhilei.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.zhilei.base.BaseInfoProperties;
import com.zhilei.mapper.ResumeEducationMapper;
import com.zhilei.pojo.ResumeEducation;
import com.zhilei.pojo.bo.EditEducationBO;
import com.zhilei.service.ResumeEducationService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;

/**
 * <p>
 * 教育经历表 服务类
 * </p>
 *
 * @author 志磊
 * @since 2025-02-05
 */
@Slf4j
@Service
public class ResumeEducationServiceImpl extends BaseInfoProperties implements ResumeEducationService {

    @Autowired
    private ResumeEducationMapper resumeEducationMapper;

    /**
     * 查询教育经历
     *
     * @param resumeEducationId 教育表的id
     * @param userId            用户id
     * @return
     */
    @Override
    public ResumeEducation getResumeEducation(String resumeEducationId, String userId) {
        ResumeEducation resumeEducation = resumeEducationMapper.selectOne(new QueryWrapper<ResumeEducation>()
                .eq("id", resumeEducationId)
                .eq("user_id", userId));
        return resumeEducation;
    }

    /**
     * 保存、编辑教育经历
     *
     * @param editEducationBO 保存教育经历bo类
     * @return
     */
    @Transactional
    @Override
    public void resumeEducation(EditEducationBO editEducationBO) {
        String id = editEducationBO.getId();
        String userId = editEducationBO.getUserId();
        String resumeId = editEducationBO.getResumeId();

        ResumeEducation resumeEducation = new ResumeEducation();
        BeanUtils.copyProperties(editEducationBO, resumeEducation);

        if (StringUtils.isBlank(id)) {
            // 新增操作
            resumeEducation.setCreateTime(LocalDateTime.now());
            resumeEducation.setUpdatedTime(LocalDateTime.now());
            resumeEducationMapper.insert(resumeEducation);
        } else {
            // 编辑操作
            resumeEducation.setUpdatedTime(LocalDateTime.now());
            resumeEducationMapper.update(resumeEducation, new QueryWrapper<ResumeEducation>()
                    .eq("id", id)
                    .eq("user_id", userId)
                    .eq("resume_id", resumeId));
        }

        redis.del(REDIS_RESUME_INFO + ":" + userId);
    }

    /**
     * 删除教育经历
     *
     * @param resumeEducationId 教育表的id
     * @param userId            用户表的id
     * @return
     */
    @Transactional
    @Override
    public void deleteResumeEducation(String resumeEducationId, String userId) {
        resumeEducationMapper.delete(new QueryWrapper<ResumeEducation>()
                .eq("id", resumeEducationId)
                .eq("user_id", userId));
        redis.del(REDIS_RESUME_INFO + ":" + userId);
    }
}
